//
//  MFWXAuthUtils.m
//  MFWSdk
//
//  Created by Song Xiaofeng on 13-7-26.
//  Copyright (c) 2013年 Mafengwo Inc. All rights reserved.
//

#import "MFWXAuthUtils.h"
#import "NSString+EnDeCoding.h"

@implementation MFWXAuthUtils

+ (NSMutableDictionary*)signatureAuthParams:(NSMutableDictionary*)orignalParams
                                 requestURL:(NSString*)requestURL
                              requesMethod:(NSString*)httpMethod
                        withAuthConsumerKey:(NSString*)consumerKey
                             consumerSecret:(NSString*)consumerSecret
                                accessToken:(NSString*)accessToken
                                tokenSecret:(NSString*)tokenSecret;
{
//    NSAssert(![MFWStringUtils isEmptyString:consumerKey] , @"consmerKey is empty");
//    NSAssert(![MFWStringUtils isEmptyString:consumerSecret], @"consumerSecret is empty");
    NSAssert(![MFWStringUtils isEmptyString:requestURL], @"request url is nil");
    
    if(!orignalParams) {
        orignalParams = [NSMutableDictionary dictionaryWithCapacity:16];
    }
    
    [orignalParams removeObjectForKey:@"oauth_token"];
    [orignalParams removeObjectForKey:@"oauth_consumer_key"];
    [orignalParams removeObjectForKey:@"oauth_signature_method"];
    [orignalParams removeObjectForKey:@"oauth_timestamp"];
    [orignalParams removeObjectForKey:@"oauth_nonce"];
    [orignalParams removeObjectForKey:@"oauth_version"];
    [orignalParams removeObjectForKey:@"oauth_signature"];
    
    NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];
    NSString *nonce = [NSString stringWithFormat:@"%@%f%ld", [NSString uuidStr], timeStamp, random()];
    nonce = [nonce md5Encode];
    
    if (![MFWStringUtils isEmptyString:accessToken])
    {
//        [orignalParams setValue:accessToken forKey:@"oauth_token"];
    }
    
//    [orignalParams setValue:consumerKey forKey:@"oauth_consumer_key"];
//    [orignalParams setValue:@"HMAC-SHA1" forKey:@"oauth_signature_method"];
//    [orignalParams setValue:[NSString stringWithFormat:@"%lld", (long long)timeStamp] forKey:@"oauth_timestamp"];
//    [orignalParams setValue:nonce forKey:@"oauth_nonce"];
//    [orignalParams setValue:@"1.0" forKey:@"oauth_version"];
//    [orignalParams setValue:@"client_auth" forKey:@"x_auth_mode"];
    
    //sort key-value pares using key order
    NSArray *allSortedKeys = [[orignalParams allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
    NSMutableString *sortedQueryStr = [NSMutableString string];
    
    for (NSString *key in allSortedKeys)
    {
        if ([sortedQueryStr length] > 0)
        {
            [sortedQueryStr appendString:@"&"];
        }
        
        NSString *value = [orignalParams objectForKey:key];
        [sortedQueryStr appendFormat:@"%@=%@", [key stringByEscapingForURLArgument], [value stringByEscapingForURLArgument]];
    }
    
    //build base string
    NSMutableString *baseString = [NSMutableString string];
    [baseString appendString:httpMethod];
    [baseString appendString:@"&"];
    [baseString appendString:[requestURL stringByEscapingForURLArgument]];
    [baseString appendString:@"&"];
    [baseString appendString:[sortedQueryStr stringByEscapingForURLArgument]];
    
//    DLog(@"baseString>> %@", baseString);
    
    //build signature secret string
    if ([MFWStringUtils isEmptyString:tokenSecret])
    {
        tokenSecret = @"";
    }
    NSString *signSecret = [NSString stringWithFormat:@"%@&%@", [consumerSecret stringByEscapingForURLArgument], [tokenSecret stringByEscapingForURLArgument]];
    
    
    NSString *signatureStr = [self oauthSignText:baseString withSecret:signSecret];
    
    [orignalParams setValue:signatureStr forKey:@"oauth_signature"];
    
    return orignalParams;
}

+ (NSString*)oauthSignText:(NSString*)text withSecret:(NSString*)secertStr;
{
    return [MFWStringUtils hmac_sha1:secertStr text:text];
}
@end
